package com.example.java.kafka.es7client;

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

public class CreateIndex {
    public static void main(String[] args) {
        ConnectElasticsearch.connect(client -> {
            try {
                // 创建索引 - 请求对象
                CreateIndexRequest request = new CreateIndexRequest("user");
                // 发送请求，获取响应
                CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
                // 响应状态
                System.out.println("Create index response: " + response.isAcknowledged());
            }catch (Exception e) {
                if (e instanceof ElasticsearchStatusException) {
                    System.out.println("Create Index`s already exists");
                }else {
                    e.printStackTrace();
                }
            }
        });
    }
}